﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmItem : Form
    {
        private List<string> GvCategory = new List<string>();
        private List<string> GvSubcategory = new List<string>();
        DataTable GvList = new DataTable("ItemDetails");
        private List<string> GvItem = new List<string>();

        public frmItem()
        {
            InitializeComponent();
            gbxpvbvdisprice.Visible = false;

            #region GvList--Datatable           
            GvList.Columns.Add(new DataColumn("Action", typeof(bool)));
            GvList.Columns.Add("Pro_ID");
            GvList.Columns.Add("Cat_Name");
            GvList.Columns.Add("Sub_Cat_Name");
            GvList.Columns.Add("Brand_Name");
            GvList.Columns.Add("Unit");
            GvList.Columns.Add("Unit_Description");
            GvList.Columns.Add("Description");
            GvList.Columns.Add("Pro_Name");
            GvList.Columns.Add("Status");
            GvList.Columns.Add("Date_Create");
            GvList.Columns.Add("User_Modify");
            GvList.Columns.Add("Last_Modify");
            GvList.Columns.Add("Barcode");
            


            #endregion
        }
        private void frmItem_Load(object sender, EventArgs e)
        {
            //Item List
            FillGridExistingProduct();
            

            //Category Combo Box
            FillGridExistingCategories();

            //Sub Category Combo box
            FillSubCatGrid();

            //Product ID
            this.txtItemCode.Text = ProductDAO.GetNewProduct();
            //PackageID
            txtPackageID.Text = PackageDAO.GetNewPackage();
           
        }
        private void Tab2_Click(object sender, EventArgs e)
        {
            //Item List
            FillGridExistingProduct();
            //PackageID
            txtPackageID.Text = PackageDAO.GetNewPackage();


        }
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            string LvBarcode = null;
            List<Product> LvProduct = ProductDAO.GetAllProduct();
            if (txtBarcode.Text != null)
            {
                foreach (Product Lvproduct in LvProduct)
                {
                    LvBarcode = Lvproduct.BarCode.ToString();
                    if (LvBarcode == txtBarcode.Text)
                    {
                        txtBarcode.Text = "";
                        MessageBox.Show("This Product Alredy Added to System");
                    }
                }

            }
        }
       
        private void FillGridExistingCategories()
        {
            //Fill the DataGrid
            List<Category> LvList = CategoryDAO.GetAllCategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;



            LvTable.Columns.Add("CategoryID ");

            LvTable.Columns.Add("CategoryName ");



            foreach (Category LvCategory in LvList)
            {
                //LvTable = (DataTable)dgvSubCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());


                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() + "-" + LvCategory.Cat_Name.ToString();


            }

            this.cmdItemcat.DisplayMember = "CategoryID ";

            this.cmdItemcat.DataSource = LvTable;



        }
        private void FillSubCatGrid()
        {
            //Fill the DataGrid


            string Lvcatid = cmdItemcat.Text.Substring(0, 7);
            List<Sub_Category> LvList1 = Sub_CategoryDAO.GetSubcategoriesByCatID(Lvcatid);
            DataTable LvTable1 = new DataTable();
            //dgvGRN.DataSource = LvTable3;



            LvTable1.Columns.Add("SubCategoryID ");

            LvTable1.Columns.Add("SubCategoryName ");



            foreach (Sub_Category LvSubCategory in LvList1)
            {
                //LvTable = (DataTable)dgvSubCategory.DataSource;

                LvTable1.Rows.Add(LvTable1.NewRow());


                LvTable1.Rows[LvTable1.Rows.Count - 1][0] = LvSubCategory.Subcat_ID.ToString() + "-" + LvSubCategory.Sub_Cat_Name.ToString();


            }

            this.cmdSubCat.DisplayMember = "SubCategoryID ";
            this.cmdSubCat.DataSource = LvTable1;

        }
        private void ItemForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {

                if (txtBarcode.Text != null)
                {

                    if (txtRetailprice.Text != "" && txtCost.Text != "")
                    {
                        gbxpvbvdisprice.Visible = true;
                        List<Price_List> LvPricelist = new List<Price_List>();
                        LvPricelist = Price_ListDAO.CalculationItemDetails(Convert.ToDecimal(txtRetailprice.Text), Convert.ToDecimal(txtCost.Text));
                        foreach (Price_List item in LvPricelist)
                        {
                            txtPV.Text = item.PV.ToString();
                            txtBV.Text = item.BV.ToString();
                            txtDisPrice.Text = item.Pro_Price.ToString();
                        }

                    }
                    else
                    {
                        MessageBox.Show("retail or cost  Price missing");
                    }

                }
            }
           
	
           
        }
        private void cmdItemcat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmdItemcat.Text != null)
            {
                FillSubCatGrid();
            }
        }
     
        #region ADD ITEM
        #region Save
        private void btnItemSave_Click(object sender, EventArgs e)
        {
            if (txtBarcode.Text != null)
            {
                int result = SaveButton();
                if (result != 0)
                {
                    MessageBox.Show("ITEM WAS SUCCESFULLY SAVED");
                }
            }
        }

        private Product CollectItemFigures()
        {
            try
            {
                Product LvItem = new Product();
                LvItem.Brand_Name = txtItemBrand.Text;
                LvItem.Cat_ID = cmdItemcat.Text;
                LvItem.Date_Create = System.DateTime.Now;
                LvItem.Description = rtxtDescription.Text;
                LvItem.Last_Modify = System.DateTime.Now;
                LvItem.Pro_ID = txtItemCode.Text;
                LvItem.Pro_Name = txtItem.Text;
                LvItem.Status = cmbStatus.Text;
                LvItem.Subcat_ID = cmdSubCat.Text;
                LvItem.Unit = Convert.ToDecimal(cmbUnit.Text);
                LvItem.Unit_Description = txtUnitDescription.Text;
                LvItem.User_Id = "1";
                LvItem.User_Modify = "1";
                LvItem.BarCode = txtBarcode.Text;
                return LvItem;

            }
            catch (Exception)
            {
                return null;
            }
        }

        private Price_List CollectPriceListFigures()
        {
            try
            {
                Price_List LvItemList = new Price_List();
                LvItemList.BV = Convert.ToDecimal(txtBV.Text);
                LvItemList.Date_Create = System.DateTime.Now;
                LvItemList.Date_Modify = System.DateTime.Now;
                LvItemList.Exp_Date = Convert.ToDateTime(dtpExpdate.Text);
                LvItemList.PL_Id = Price_ListDAO.GetNewPriceListID();
                LvItemList.Pro_ID = txtItem.Text;
                LvItemList.Pro_Price = Convert.ToDecimal(txtDisPrice.Text);
                LvItemList.Pur_price = Convert.ToDecimal(txtCost.Text);
                LvItemList.PV = Convert.ToDecimal(txtPV.Text);
                LvItemList.Ret_Price = Convert.ToDecimal(txtRetailprice.Text);
                LvItemList.User_Id = "1";
                LvItemList.User_Modify = "1";
                return LvItemList;
            }
            catch (Exception)
            {
                return null;
            }

        }

        private int SaveButton()
        {
            Price_List LvItemPL = CollectPriceListFigures();
            Product LvItem = CollectItemFigures();
            if (LvItem != null && LvItemPL != null)
            {
                return ProductDAO.SaveItem(LvItem, LvItemPL);
            }
            else
            {
                return 0;
            }
        }

        #endregion

        #region New

        private void btnItemNew_Click(object sender, EventArgs e)
        {
            //Sub Category Combo box
           FillSubCatGrid();

            //Category Combo Box
           FillGridExistingCategories();

            //Product ID
            this.txtItem.Text = ProductDAO.GetNewProduct();
        }
        #endregion

        #region Clear
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtItemBrand.Text = "";
            cmdItemcat.Text = "";
            rtxtDescription.Text = "";
            txtItemCode.Text = "";
            txtItem.Text = "";
            cmbStatus.Text = "";
            cmdSubCat.Text = "";
            cmbUnit.Text = "";
            txtUnitDescription.Text = "";
        }
        #endregion

       
        #endregion

        #region Package

        #region NEW
        private void btnPackageNew_Click(object sender, EventArgs e)
        {
            //Package ID
            txtPackageID.Text = PackageDAO.GetNewPackage();

            //Item List
            FillGridExistingProduct();
        }

        private void FillGridExistingProduct()
        {
            //Fill the DataGrid
            List<Product> LvList = ProductDAO.GetAllProduct();
            DataTable LvTable = new DataTable();

            LvTable.Columns.Add("Pro_ID ");
            LvTable.Columns.Add("Pro_Name ");


            foreach (Product LvProduct in LvList)
            {


                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvProduct.Pro_ID.ToString() + "-" + LvProduct.Pro_Name.ToString();

            }

            this.cmdPackageItem.DisplayMember = "Pro_ID ";
            this.cmdPackageItem.DataSource = LvTable;

        }

        #endregion
       
        #region Add
        private void btnPackageAdd_Click(object sender, EventArgs e)
        {
            if (cmdPackageItem.Text != null)
            {
                string LvItemid = cmdPackageItem.Text.Substring(0, 7);


              
                DataTable LvList = new DataTable();
                LvList = ProductDAO.GetAllProductByProID(LvItemid);

                for (int i = 0; i <= LvList.Rows.Count - 1; i++)
                {
                    GvList.Rows.Add(GvList.NewRow());

                    GvList.Rows[GvList.Rows.Count - 1][1] = LvList.Rows[i][0].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][2] = LvList.Rows[i][1].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][3] = LvList.Rows[i][2].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][4] = LvList.Rows[i][3].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][5] = LvList.Rows[i][4].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][6] = LvList.Rows[i][5].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][7] = LvList.Rows[i][6].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][8] = LvList.Rows[i][7].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][9] = LvList.Rows[i][8].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][10] = LvList.Rows[i][9].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][11] = LvList.Rows[i][10].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][12] = LvList.Rows[i][11].ToString();
                    GvList.Rows[GvList.Rows.Count - 1][13] = LvList.Rows[i][12].ToString();
                   

                }
                gdvItemList.DataSource = GvList;

            }
        }

        #endregion

        //private void btnPackageAdding_Click(object sender, EventArgs e)
        //{

        //    DataSet ds = new DataSet("GvList");

        //    string detailsXML = "";
        #endregion

        //    ds.Tables.Add(GvList.Copy());
        //    detailsXML = ds.GetXml();

        //    SavePR(detailsXML);
        //    MessageBox.Show("Package Was Succesfuly Saved");
        //}
        //#endregion
        //#endregion

    //    private int SavePR(string xmlstring)
    //    {
    //        try
    //        {
    //            string Lvxml = xmlstring;

    //            if (gdvItemList.RowCount != 0)
    //            {
    //                for (int i = 0; i < gdvItemList.RowCount - 1; i++)
    //                {
    //                    string LvPLID = gdvItemList.Rows[i].Cells[4].Value.ToString();
    //                    GvItem.Add(LvPLID);
    //                    Package LvPriceList = CollectPriceListFigures(i);
    //                    if (LvPriceList != null)
    //                    {
    //                        return PackageDAO.SavePackage(LvPriceList);
    //                    }
    //                    else
    //                    {
    //                        return 0;
    //                    }
    //                }
    //            }
               
    //        }
    //        catch
    //        {
    //            return 0;
    //        }
    //    }

    //    private Package CollectPriceListFigures(int i)
    //    {

    //        try
    //        {
    //            Package LvPackage = new Package();
    //            LvPackage.Last_Modify = System.DateTime.Now;
    //            LvPackage.Date_Create = System.DateTime.Now;
    //            LvPackage.Package_ID = txtPackageID.Text;
    //            foreach (string Lvitem in GvItem)
    //{
    //    LvPackage.PL_ID = Lvitem[i].ToString();
    //}

    //            return LvPackage;
    //        }
    //        catch (Exception)
    //        {
    //            return null;
    //        }
    //    }
        //#endregion




    }

        //private void txtBarcode_TextChanged(object sender, EventArgs e)
        //{


        //}
}

